home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / MMATH.H < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-01  |  3.1 KB  |  148 lines

  1. /* $Id: mmath.h,v 3.1 1998/04/18 05:00:14 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: mmath.h,v $
  26.  * Revision 3.1  1998/04/18 05:00:14  brianp
  27.  * added FLOAT_COLOR_TO_UBYTE_COLOR macro
  28.  *
  29.  * Revision 3.0  1998/01/31 20:59:27  brianp
  30.  * initial rev
  31.  *
  32.  */
  33.  
  34.  
  35. /*
  36.  * Faster arithmetic functions.  If the FAST_MATH preprocessor symbol is
  37.  * defined on the command line (-DFAST_MATH) then we'll use some (hopefully)
  38.  * faster functions for sqrt(), etc.
  39.  */
  40.  
  41.  
  42. #ifndef MMATH_H
  43. #define MMATH_H
  44.  
  45.  
  46. #include <math.h>
  47.  
  48.  
  49.  
  50. /*
  51.  * Set the x86 FPU control word to less precision for more speed:
  52.  */
  53. #if defined(__linux__) && defined(__i386__) && defined(FAST_MATH)
  54. #include <i386/fpu_control.h>
  55. #define START_FAST_MATH  __setfpucw(_FPU_SINGLE | _FPU_MASK_IM | _FPU_MASK_DM \
  56.             | _FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM | _FPU_MASK_PM);
  57. #define END_FAST_MATH  __setfpucw(_FPU_EXTENDED | _FPU_MASK_IM | _FPU_MASK_DM \
  58.             | _FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM | _FPU_MASK_PM);
  59. #else
  60. #define START_FAST_MATH
  61. #define END_FAST_MATH
  62. #endif
  63.  
  64.  
  65.  
  66. /*
  67.  * Float -> Int conversion
  68.  */
  69.  
  70. #if defined(USE_X86_ASM)
  71. static __inline__ int FloatToInt(float f)
  72. {
  73.    int r;
  74.    __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
  75.    return r;
  76. }
  77. #else
  78. #define FloatToInt(F) ((int) (F))
  79. #endif
  80.  
  81.  
  82.  
  83. /*
  84.  * Square root
  85.  */
  86.  
  87. extern float gl_sqrt(float x);
  88.  
  89. #ifdef FAST_MATH
  90. #  define GL_SQRT(X)  gl_sqrt(X)
  91. #else
  92. #  define GL_SQRT(X)  sqrt(X)
  93. #endif
  94.  
  95.  
  96.  
  97. /*
  98.  * Normalize a 3-element vector to unit length.
  99.  */
  100. #define NORMALIZE_3FV( V )                \
  101. {                            \
  102.    GLfloat len;                        \
  103.    len = GL_SQRT(V[0]*V[0]+V[1]*V[1]+V[2]*V[2]);    \
  104.    if (len>0.0001F) {                    \
  105.       len = 1.0F / len;                    \
  106.       V[0] *= len;                    \
  107.       V[1] *= len;                    \
  108.       V[2] *= len;                    \
  109.    }                            \
  110. }
  111.  
  112.  
  113.  
  114. /*
  115.  * Optimization for:
  116.  * GLfloat f;
  117.  * GLubyte b = FloatToInt(CLAMP(f, 0, 1) * 255)
  118.  */
  119.  
  120. #if defined(__i386__)
  121. #define USE_IEEE
  122. #endif
  123.  
  124. #if defined(USE_IEEE) && !defined(DEBUG)
  125.  
  126. #define IEEE_ONE 0x3f7f0000
  127. #define FLOAT_COLOR_TO_UBYTE_COLOR(b, f)            \
  128.     {                            \
  129.        GLfloat tmp = f + 32768.0F;                \
  130.        b = ((*(GLuint *)&f >= IEEE_ONE)            \
  131.            ? (*(GLint *)&f < 0) ? (GLubyte)0 : (GLubyte)255    \
  132.            : (GLubyte)*(GLuint *)&tmp);            \
  133.     }
  134.  
  135. #else
  136.  
  137. #define FLOAT_COLOR_TO_UBYTE_COLOR(b, f)            \
  138.     b = FloatToInt(CLAMP(f, 0.0F, 1.0F) * 255.0F)
  139.  
  140. #endif
  141.  
  142.  
  143.  
  144. extern void gl_init_math(void);
  145.  
  146.  
  147. #endif
  148.